package com.kx.center.controller;

import java.security.GeneralSecurityException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kx.center.entity.Account;
import com.kx.center.entity.StoreAppAccount;
import com.kx.center.entity.Suggestion;
import com.kx.center.enumerate.MessageCode;
import com.kx.center.exception.AccountAlreadyExistsException;
import com.kx.center.service.StoreAppAccountService;
import com.kx.center.util.Response;
import com.kx.center.util.StringUtil;

/**
 * 
 * @description 门店端app账户controller
 * @version 1.0
 * @author Scott
 * @date 2015年12月17日 上午11:43:31
 */
@Controller
@RequestMapping(value = "/appAccount")
public class StoreAppAccountController {

	@Autowired
	StoreAppAccountService accountService;

	@RequestMapping(value = "/register", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
	@ResponseBody
	public String register(HttpServletRequest request) {
		String userId = request.getParameter("userId");
		String password = request.getParameter("password");
		String userName = request.getParameter("user_name");
		StoreAppAccount appAccount = new StoreAppAccount();
		appAccount.setUserId(userId);
		appAccount.setPassword(password);
		appAccount.setUsername(userName);
		appAccount.setStoreInfoId(1);
		appAccount.setRoleId(1);

		try {
			accountService.createAccount(appAccount);
		} catch (AccountAlreadyExistsException e) {
			// 账户已存在
			e.printStackTrace();
			return Response.error(MessageCode.ACCOUNT_AREADT_EXIT);
		} catch (GeneralSecurityException e) {
			//服务器异常
			e.printStackTrace();
			return Response.error(MessageCode.SERVER_ERROR);
		}

		return Response.success("注册成功");
	}

	/**
	 * @description 门店端app登录 @version 1.0 @author Scott @date 2015年12月17日
	 * 下午6:35:30 @param request @return String @throws
	 */
	@RequestMapping(value = "/login", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
	@ResponseBody
	public String login(HttpServletRequest request) {

		try {
			String userId = request.getParameter("userId").trim();
			String password = request.getParameter("password").trim();
			HashMap<String, Object> resultMap = accountService.findUserByUserIdAndPassword(userId, password);

			if (resultMap != null) {
				return Response.success("用户合法", resultMap);
			}
		} catch (NullPointerException e) {
			e.printStackTrace();
			return Response.error(MessageCode.PARAMETER_WRONG);
		} catch (Exception e) {
			e.printStackTrace();
			return Response.error(MessageCode.PARAMETER_WRONG);
		}
		return Response.error("用户名或密码不正确");

	}
	/**
	 * @description 修改密码
	 * @version 1.0
	 * @author 李梦龙 
	 * @date 2016年5月24日 上午11:19:00
	 * @param request
	 * @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/changePassword", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
	@ResponseBody
	public String updatePassword(HttpServletRequest request){
		//获取用户id
		String userId = request.getParameter("userId");
		String oldPassword = request.getParameter("old_password");
		String newPassword = request.getParameter("new_password");
		
		if(StringUtils.isEmpty(userId) || StringUtils.isEmpty(oldPassword) || StringUtils.isEmpty(newPassword)){
			return Response.error(MessageCode.PARAMETER_WRONG);
		}
		
		//根据用户id判断原密码是否合法
		boolean isTrue = accountService.checkOldPassword(userId,oldPassword);
		if(!isTrue){
			//原密码不合法
			return Response.error(MessageCode.OLDPASSWORD_WRONG);
		}
		//原密码正确 修改密码
		accountService.changePasswordByUserId (userId,newPassword);
		return Response.success("修改成功");
	}
	
	/**
	 * @description 获取账户列表
	 * @version 1.0
	 * @author 李梦龙 
	 * @date 2016年5月25日 下午1:39:23
	 * @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/accountList/{pageNow}/{pageSize}", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
	@ResponseBody
	public String accountList(HttpServletRequest request ,@PathVariable("pageNow")String pageNow ,@PathVariable("pageSize")String pageSize){
		if(!NumberUtils.isDigits(pageNow) || !NumberUtils.isDigits(pageSize)){
			return Response.error(MessageCode.PARAMETER_WRONG);
		}
		
		
		List<StoreAppAccount> accountList = accountService.findAccountListByPage(NumberUtils.toInt(pageNow), NumberUtils.toInt(pageSize));
		
		//查询总共有多少条
		int pageCount = accountService.findPageCount();
		
		HashMap<String, Object> map = new HashMap<>();
		map.put("datas", accountList);
		map.put("pageNow", pageNow);
		map.put("pageSize", pageSize);
		map.put("pageCount", pageCount);
		return Response.success(map);
	}
	
	/**
	 * @description 根据id删除(修改状态为失效) 
	 * @version 1.0
	 * @author 李梦龙 
	 * @date 2016年5月27日 下午3:19:37
	 * @param request
	 * @param accountId
	 * @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/accountDel/{accountId}", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
	@ResponseBody
	public String removeAccount(HttpServletRequest request ,@PathVariable("accountId") String accountId){
		
		if(!NumberUtils.isDigits(accountId) || NumberUtils.toInt(accountId) <=0 ){
			//账户id不合法
			Response.error(MessageCode.PARAMETER_WRONG);
		}
		accountService.deleteAccount(NumberUtils.toInt(accountId));
		
		return Response.success("删除成功");
	}
	
	
	
	
}









